package one.bugu.android.demon.ui.fragment;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.TextView;
import android.widget.Toast;

import com.juefeng.android.framework.LKUtil;
import com.juefeng.android.framework.view.annotation.LKContentView;
import com.juefeng.android.framework.view.annotation.LKInjectView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import one.bugu.android.demon.R;
import one.bugu.android.demon.bean.CoinBean;
import one.bugu.android.demon.bean.SearchBean;
import one.bugu.android.demon.common.BaseHttpAsycResponceHandler;
import one.bugu.android.demon.common.MyBaseFragment;
import one.bugu.android.demon.constant.Constant;
import one.bugu.android.demon.constant.HttpConstant;
import one.bugu.android.demon.ui.adapter.MyExtendableListViewAdapter;
import one.bugu.android.demon.util.PreferencesUtil;

/**
 * 搜索结果界面
 */
@LKContentView(R.layout.expandablelistview)
public class SearchResultFragment extends MyBaseFragment {

    /**
     * 展示搜索结果的列表  二级列表
     */
    @LKInjectView(R.id.expend_list)
    private ExpandableListView expend_list;

    private MyExtendableListViewAdapter myExtendableListViewAdapter;

    private String token;

    /**
     * 编辑框输入的文字
     */
    private String keyword;

    private long rowId;

    /**
     * 搜索无结果显示的文字
     */
    @LKInjectView(R.id.res_none)
    private TextView resNone;

    @Override
    protected void initView() {
        super.initView();

        View view = LayoutInflater.from(getActivity()).inflate(R.layout.view_footer, null);
        expend_list.addFooterView(view);

        myExtendableListViewAdapter = new MyExtendableListViewAdapter(getActivity());

        expend_list.setAdapter(myExtendableListViewAdapter);

        //去除父节点图标
        expend_list.setGroupIndicator(null);


        token = PreferencesUtil.getInstance().getString(getContext(), Constant.TOKEN);
        keyword = PreferencesUtil.getInstance().getString(getContext(), Constant.KEYWORD);

        getResult();
    }

    @Override
    protected void initData() {
        super.initData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        //设置列表父级不可点击
        expend_list.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
            @Override
            public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
                return true;//返回true,表示不可点击
            }
        });

    }

    /**
     * 搜索结果展示方法
     */
    public void getResult() {

        Map<String, String> map = new HashMap<>();
        map.put("keyword", keyword);
        map.put("token", token);
        LKUtil.getHttpManager().postBody(HttpConstant.SOUSUO_URL, map, handler);
    }

    /**
     * 搜索结果网络请求
     */
    @SuppressLint("HandlerLeak")
    public BaseHttpAsycResponceHandler<List<SearchBean>> handler = new BaseHttpAsycResponceHandler<List<SearchBean>>(true) {

        @Override
        public void onSuccess(List<SearchBean> dataBeans) {
            super.onSuccess(dataBeans);

            //返回数据为空 显示无结果的文字
            if (dataBeans == null || dataBeans.isEmpty()){

                resNone.setVisibility(View.VISIBLE);
                expend_list.setVisibility(View.GONE);
            }else {

                resNone.setVisibility(View.GONE);
                expend_list.setVisibility(View.VISIBLE);
            }

            myExtendableListViewAdapter.setList(dataBeans);

            myExtendableListViewAdapter.notifyDataSetChanged();
            int groupCount = expend_list.getCount();

            for (int i = 0; i < groupCount; i++) {
                expend_list.expandGroup(i);
            }
        }

        @Override
        public void onFailed(int code, String message) {
            super.onFailed(code, message);

        }

        @Override
        public void onError() {
            super.onError();
        }
    };
}
